//
//  YTAddDriverViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/18.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTAddDriverViewController.h"
#import "YTDriverTeamServices.h"

@interface YTAddDriverViewController ()
@property (weak, nonatomic) IBOutlet UITextField *driverNameText;
@property (weak, nonatomic) IBOutlet UITextField *driverTelText;
@property (weak, nonatomic) IBOutlet UITextField *driverTel2Text;
@property (weak, nonatomic) IBOutlet UITextField *driverWechatText;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *completionBtn;
@end

@implementation YTAddDriverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    id (^notEmpty)(NSString *str) = ^id (NSString *str) {
        return @(str.length > 0);
    };
    RACSignal *name = [_driverNameText.rac_textSignal map:notEmpty];
    RACSignal *tel = [_driverTelText.rac_textSignal map:notEmpty];
    RACSignal *tel2 = [_driverTel2Text.rac_textSignal map:notEmpty];
    RACSignal *wechat = [_driverWechatText.rac_textSignal map:notEmpty];
    
    RAC(_completionBtn, enabled) = [RACSignal combineLatest:@[name, tel, tel2, wechat]
                                                     reduce:^id(NSNumber *enable0, NSNumber *enable1,
                                                                NSNumber *enable2, NSNumber *enable3){
         return @(enable0.boolValue && enable1.boolValue && enable2.boolValue && enable3.boolValue);
    }];
}

- (IBAction)addCompletionAction:(UIBarButtonItem *)sender {
    [SVProgressHUD show];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"realName"] = _driverNameText.text;
    dict[@"phone"] = _driverTelText.text;
    dict[@"phone2"] = _driverTel2Text.text;
    dict[@"wechat"] = _driverWechatText.text;
    [[YTDriverTeamServices addDriverToTeam:dict] subscribeNext:^(NSDictionary *response) {
        [SVProgressHUD showSuccessWithStatus:@"添加成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } error:^(NSError *error) {
        [SVProgressHUD dismiss];
    } completed:^{
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - Navigation
 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

}

@end
